<?php

class AjaxController extends Zend_Controller_Action {

	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;

	//Thong so phan trang
	protected $_paginator = array('itemCountPerPage' => 5, 'pageRange' => 3);

	public function init() {

		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();

		$this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
		$this->_arrParam['paginator'] = $this->_paginator;

		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		
	}

	public function districtAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		
		if (isset($this->_arrParam)) {
			$selectedCountry = $this->_request->getQuery('selectedValue');
			$modelDistricts = new Default_Model_QuanHuyenMapper();
			$district = $modelDistricts->fetchAll('thanhpho = ' . $this->_arrParam['id']);
			$arr = '';
			$i = 0;
			$arr[$i]['id'] = '0';
			$arr[$i]['ten'] = ' -- Quận huyện -- ';
			foreach ($district as $val) {
				$i++;
				$arr[$i]['id'] = $val->id;
				$arr[$i]['ten'] = $val->ten;
			}

			echo json_encode($arr);
		} else {
			echo "wrong check";
		}
	}

	public function goivipAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		
		if (isset($this->_arrParam)) {
			$model = new Default_Model_GoiTinMapper();
			$goi_tin = $model->fetchAll('id = ' . $this->_arrParam['id']);
			foreach ($goi_tin as $item) {
				$arr[0] = number_format($item->gia_tien, 0, ",", ",");
				$arr[1] = $item->so_ngay;
			}

			echo json_encode($arr);
		} else {
			echo "wrong check";
		}
	}

	// Tin đăng nhà trọ
	public function ptrtinraoAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		
		if (isset($_POST)) {
			$loai_tin = $this->_arrParam['type'];
			$id_tab = $this->_arrParam['idTab'];
			$paginator = new Zendhlt_Paginator();
			
			$model = new Default_Model_PtrTinRaoMapper();
			$items = $model->fetch($this->_arrParam, 'loai_tin = ' . $loai_tin . ' AND goi_tin = 1 AND trang_thai = 1', 0, array(array('colName' => 'ngay_dang', 'order' => 'DESC')), 'list');
			
			$html = '';
			foreach ($items as $val) {
				$htmlMetadata = '';
				$htmlDate = '';
				$htmlConPhong = '';
				
				$id_tin_rao	= $val->id;
				$tieu_de 	= $val->tieu_de;
				$luot_xem 	= $val->luot_xem;
				$ngay_dang 	= $val->ngay_dang;
				
				// Kiểm tra loại tin là tìm người ở trọ hay tìm phòng trọ để thay đổi giao diện
				if ($loai_tin == 2) {
					$img 		= $val->hinh_anh;
					$hinh_anh 	= '/phong_tro/avatar/' . $img;
					if (preg_match("/(.*)[,](.*)/", $img)) {
						$list_image = explode (',', $img);
						$hinh_anh = '/phong_tro/avatar/' . $list_image[0];
					}
					
					$htmlMetadata .= '<div class="metadata">';
					$htmlMetadata .= '<img src="' . $this->view->baseUrl(PUBLIC_IMG_URL . $hinh_anh) . '" alt="' . $val->tieu_de . '" width="64px" height="64px" onerror="this.onerror=null; this.src=\'' . $this->view->baseUrl(PUBLIC_IMG_URL . '/phong_tro/avatar/error_avatar_home.png') . '\';" />';
					$htmlMetadata .= '<ul>';
					$htmlMetadata .= '<li class="label-view">Số lượt xem</li>';
					$htmlMetadata .= '<li class="num-view">' . $luot_xem . '</li>';
					$htmlMetadata .= '</ul>';
					$htmlMetadata .= '</div>';
					
					$htmlDate .= '<div class="date post-date">' . $ngay_dang . '<div class="icon_vip"></div></div>';
					
					$con_phong 	= $val->con_phong;
					if ($con_phong)
						$htmlConPhong .= '<div class="icon_con_phong"></div>';
				} else {
					$htmlDate .= '<div class="date"><span class="post-date">' . $ngay_dang . '</span>';
					$htmlDate .= '<span class="view"><b>' . $luot_xem . '</b> Lượt xem</span><div class="icon_vip"></div></div>';
				}
				$dia_chi 	= $val->dia_chi;
				$gia_phong 	= number_format($val->gia_phong, 3, ",", ",") . ' VNĐ';
				if ($val->gia_phong == null || $val->gia_phong == 0)
					$gia_phong = 'Thỏa thuận';
				$dien_tich 	= $val->dien_tich;
				$quan_huyen	= $val->getQuanHuyenObj();
				$nguoi_dang = $val->getNguoiDangObj();
				$html .= '<div class="item">';
				$html .= $htmlMetadata;
				$html .= '<a title="' . $tieu_de . '" rel="bookmark" href="' . $this->view->baseUrl("/ptrtinrao/view/id/" . $id_tin_rao) . '">' . Zendhlt_Encode::truncateString_($tieu_de, 48). '</a>';
				$html .= $htmlDate;
				$html .= '<div class="info">';
				$html .= 'Địa chỉ: ' . $dia_chi . '<br />';
				$html .= 'Giá phòng: <span class="price">' . $gia_phong . '</span><br />';
				$html .= 'Diện tích: ' . $dien_tich . ' m<sup>2</sup><br />';
				$html .= '<div class="district">' . $quan_huyen->ten . '<br />';
				$html .= '<a class="send_email" title="Gửi email đến người đăng" href="#sendMailModal" data-toggle="modal" onclick="setVal(\'ptrtinrao\', ' . $id_tin_rao . ', ' . $nguoi_dang->id . ', \'' . $nguoi_dang->email . '\')""><img src="' . $this->view->baseUrl(DEFAULT_PATH . '/img/icon_email.png') . '" alt="' . $nguoi_dang->email . '" /></a>';
				$html .= $htmlConPhong;
				$html .= '</div>';
				$html .= '</div>';
				$html .= '<div class="clear"></div></div>';
			}
			
			// Đếm số items VIP1 của table Ptr_Tin_Rao
			$totalItem = $model->countItem(null, 'loai_tin = ' . $loai_tin . ' AND goi_tin = 1 AND trang_thai = 1');
			
			$html .= '<div class="pagenavi">';
			$html .= '<div class="pagination">';
			$panigator = $paginator->createPaginator($totalItem, $this->_paginator);
			$html .= $this->view->paginationControl($panigator, 'Sliding', 'pagination.phtml', array('ajax' => "'" . $id_tab . "', 'ptrtinrao', " . $loai_tin . ", "));
			$html .= '</div>';
			$html .= '<br class="clear" /></div>';

			$data = array('content' => $html);
			echo json_encode($data);
		} else {
			echo "wrong check";
		}
		
	}

	// Tin đăng ở ghép
	public function oghtinraoAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		
		if (isset($_POST)) {
			$loai_tin = $this->_arrParam['type'];
			$id_tab = $this->_arrParam['idTab'];
			$paginator = new Zendhlt_Paginator();
			
			$model = new Default_Model_OghTinRaoMapper();
			$items = $model->fetch($this->_arrParam, 'loai_tin = ' . $loai_tin . ' AND goi_tin = 1 AND trang_thai = 1', 0, array(array('colName' => 'ngay_dang', 'order' => 'DESC')), 'list');
			
			$html = '';
			foreach ($items as $val) {
				$id_tin_rao	= $val->id;
				$tieu_de 	= $val->tieu_de;
				$hinh_anh	= '/phong_tro/avatar/' . $val->hinh_anh;
				$luot_xem 	= $val->luot_xem;
				$ngay_dang 	= $val->ngay_dang;
				$dia_chi 	= $val->dia_chi;
				$goi_tin	= $val->goi_tin;
				$displayVIP = '';
				
				$gia_phong 	= number_format($val->gia_o_ghep, 3, ",", ",") . ' VNĐ';
				if ($val->gia_o_ghep == null || $val->gia_o_ghep == 0)
					$gia_phong = 'Thỏa thuận';
				$dien_tich 	= $val->dien_tich_phong;
				$quan_huyen	= $val->getQuanHuyenObj();
				$nguoi_dang = $val->getNguoiDangObj();
				$html .= '<div class="item">';
				$html .= '<div class="metadata">';
				$html .= '<img src="' . $this->view->baseUrl(PUBLIC_IMG_URL . $hinh_anh) . '" alt="' . $val->tieu_de . '" width="64px" height="64px" onerror="this.onerror=null; this.src=\'' . $this->view->baseUrl(PUBLIC_IMG_URL . '/phong_tro/avatar/error_avatar_home.png') . '\';" />';
				$html .= '<ul>';
				$html .= '<li class="label-view">Số lượt xem</li>';
				$html .= '<li class="num-view">' . $luot_xem . '</li>';
				$html .= '</ul>';
				$html .= '</div>';
				$html .= '<a title="' . $tieu_de . '" rel="bookmark" href="' . $this->view->baseUrl("/oghtinrao/view/id/" . $id_tin_rao) . '">' . Zendhlt_Encode::truncateString_($tieu_de, 48). '</a>';
				$html .= '<div class="date post-date">' . $ngay_dang . '<div class="icon_vip"></div></div>';
				$html .= '<div class="info">';
				$html .= 'Địa chỉ: ' . $dia_chi . '<br />';
				$html .= 'Giá phòng: <span class="price">' . $gia_phong . '</span><br />';
				$html .= 'Diện tích: ' . $dien_tich . ' m<sup>2</sup><br />';
				$html .= '<div class="district">' . $quan_huyen->ten . '<br />';
				$html .= '<a class="send_email" title="Gửi email đến người đăng" href="#sendMailModal" data-toggle="modal" onclick="setVal(\'oghtinrao\', ' . $id_tin_rao . ', ' . $nguoi_dang->id . ', \'' . $nguoi_dang->email . '\')""><img src="' . $this->view->baseUrl(DEFAULT_PATH . '/img/icon_email.png') . '" alt="' . $nguoi_dang->email . '" /></a>';
				$html .= '</div>';
				$html .= '</div>';
				$html .= '<div class="clear"></div></div>';
			}
			
			// Đếm số items VIP1 của table Ogh_Tin_Rao
			$totalItem = $model->countItem(null, 'loai_tin = ' . $loai_tin . ' AND goi_tin = 1 AND trang_thai = 1');
			
			$html .= '<div class="pagenavi">';
			$html .= '<div class="pagination">';
			$panigator = $paginator->createPaginator($totalItem, $this->_paginator);
			$html .= $this->view->paginationControl($panigator, 'Sliding', 'pagination.phtml', array('ajax' => "'" . $id_tab . "', 'oghtinrao', " . $loai_tin . ", "));
			$html .= '</div>';
			$html .= '<br class="clear" /></div>';

			$data = array('content' => $html);
			echo json_encode($data);
		} else {
			echo "wrong check";
		}
	}
	
	public function searchAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		
		if (isset($_POST)) {
			$record = '';
			if (isset($this->_arrParam['rc'])) {
				$this->_paginator['itemCountPerPage'] = $this->_arrParam['rc'];
				$record = 'rc/' . $this->_arrParam['rc'] . '/';
			}
			$loaitin = $this->_arrParam['type'];
			$id_tab = $this->_arrParam['idTab'];
			$paginator = new Zendhlt_Paginator();
			
			$query = 'trang_thai = 1';
			
			if ($this->_arrParam['tt'] > 0) {
				$query .= ' and thanh_pho = ' . $this->_arrParam['tt'];
			}
		
			if ($this->_arrParam['qh'] > 0) {
				$query .= ' and quan_huyen = ' . $this->_arrParam['qh'];
			}
		
			// Giá
			if ($this->_arrParam['g'] > 0) {
				switch ($this->_arrParam['g']) {
					case 1:
						$query .= ' and idgia between 0 and 1000';
					break;
					
					case 2:
						$query .= ' and idgia between 1000 and 2000';
					break;
					
					case 3:
						$query .= ' and idgia between 2000 and 4000';
					break;
					
					case 4:
						$query .= ' and idgia between 4000 and 6000';
					break;
					
					case 5:
						$query .= ' and idgia > 6000';
					break;
					
					default:
						$query .= ' and idgia = 0';
					break;
				}
			}
		
			// Diện tích
			if ($this->_arrParam['dt'] > 0) {
				switch ($this->_arrParam['dt']) {
					case 10:
						$query .= ' and iddientich between 0 and 20';
					break;
					
					case 11:
						$query .= ' and iddientich between 20 and 40';
					break;
					
					case 12:
						$query .= ' and iddientich between 40 and 80';
					break;
					
					case 13:
						$query .= ' and iddientich between 80 and 100';
					break;
					
					case 14:
						$query .= ' and iddientich > 100';
					break;
				}
			}
		
			// Loại phòng
			if ($this->_arrParam['lp'] > 0) {
				$query .= ' and loai_phong = ' . $this->_arrParam['lp'];
			}
		
			// Số người ở
			if ($this->_arrParam['sng'] > 0) {
				$strQuery = ' and so_nguoi_o = ' . $this->_arrParam['sng'];
				if ($this->_arrParam['sng'] == 11)
					$strQuery = ' and so_nguoi_o > ' . $this->_arrParam['sng'];
					
				$query .= $strQuery;
			}
				
			$order = array(array('colName' => 'goi_tin', 'order' => 'ASC'), array('colName' => 'ngay_dang', 'order' => 'DESC'));
			
			$model = new Default_Model_LoaiTinMapper();
			$loai_tin = $model->fetchAll('id = ' . $loaitin, 'view');
			$model = new Default_Model_PtrTinRaoMapper();
			$strGia = 'gia_phong';
			$strDienTich = 'dien_tich';
			$action = 'ptrtinrao';
			if ($loai_tin->group_loai_tin == 1) {
				$model = new Default_Model_OghTinRaoMapper();
				$strGia = 'gia_o_ghep';
				$strDienTich = 'dien_tich_phong';
				$action = 'oghtinrao';
			}
			$query = str_replace("idgia", $strGia, $query);
			$query = str_replace("iddientich", $strDienTich, $query);
			$items = $model->fetch($this->_arrParam, $query, 0, $order, 'list');
			
			$html = '';
			// Dùng kiểm tra hiển thị khung cho tin VIP
			$checkVipFirst = true;
			$checkVipEnd = false;
			foreach ($items as $val) {
				$htmlMetadata = '';
				$htmlDate = '';
				$htmlConPhong = '';
				
				$id_tin_rao	= $val->id;
				$tieu_de 	= $val->tieu_de;
				$luot_xem 	= $val->luot_xem;
				$ngay_dang 	= $val->ngay_dang;
				$goi_tin	= $val->goi_tin;
				$displayVIP = '';
				if ($goi_tin != 4) {
					$displayVIP = '<div class="icon_vip"></div>';
					if ($checkVipFirst) {
						$html .= '<div class="box-vip">';
						$checkVipFirst = false;
						$checkVipEnd = true;
					}
				} elseif ($checkVipEnd) {
					$html .= '</div>';
					$checkVipEnd = false;
				}
				
				// Kiểm tra loại tin là tìm người ở trọ hay tìm phòng trọ để thay đổi giao diện
				if ($loaitin > 1) {
					$img 		= $val->hinh_anh;
					$hinh_anh 	= '/phong_tro/avatar/' . $img;
					if (preg_match("/(.*)[,](.*)/", $img)) {
						$list_image = explode (',', $img);
						$hinh_anh = '/phong_tro/avatar/' . $list_image[0];
					}
					
					$htmlMetadata .= '<div class="metadata">';
					$htmlMetadata .= '<img src="' . $this->view->baseUrl(PUBLIC_IMG_URL . $hinh_anh) . '" alt="' . $val->tieu_de . '" width="64px" height="64px" onerror="this.onerror=null; this.src=\'' . $this->view->baseUrl(PUBLIC_IMG_URL . '/phong_tro/avatar/error_avatar_home.png') . '\';" />';
					$htmlMetadata .= '<ul>';
					$htmlMetadata .= '<li class="label-view">Số lượt xem</li>';
					$htmlMetadata .= '<li class="num-view">' . $luot_xem . '</li>';
					$htmlMetadata .= '</ul>';
					$htmlMetadata .= '</div>';
					
					$htmlDate .= '<div class="date post-date">' . $ngay_dang . $displayVIP . '</div>';
					
					if ($loai_tin->group_loai_tin == 0) {
						$con_phong 	= $val->con_phong;
						if ($con_phong)
							$htmlConPhong .= '<div class="icon_con_phong"></div>';
					}
				} else {
					$htmlDate .= '<div class="date"><span class="post-date">' . $ngay_dang . '</span>';
					$htmlDate .= '<span class="view"><b>' . $luot_xem . '</b> Lượt xem</span><div class="icon_vip"></div></div>';
				}
				$dia_chi 	= $val->dia_chi;
				if ($loai_tin->group_loai_tin == 0) {
					$gia = $val->gia_phong;
					$dien_tich 	= $val->dien_tich;
				} else {
					$gia = $val->gia_o_ghep;
					$dien_tich 	= $val->dien_tich_phong;
				}
				$gia_phong 	= number_format($gia, 3, ",", ",") . ' VNĐ';
				if ($gia == null || $gia == 0)
					$gia_phong = 'Thỏa thuận';
				$quan_huyen	= $val->getQuanHuyenObj();
				$nguoi_dang = $val->getNguoiDangObj();
				$email 		= $nguoi_dang->email;
				$html .= '<div class="item">';
				$html .= $htmlMetadata;
				$html .= '<a title="' . $tieu_de . '" rel="bookmark" href="' . $this->view->baseUrl("/" . $action . "/view/id/" . $id_tin_rao) . '">' . Zendhlt_Encode::truncateString_($tieu_de, 48). '</a>';
				$html .= $htmlDate;
				$html .= '<div class="info">';
				$html .= 'Địa chỉ: ' . $dia_chi . '<br />';
				$html .= 'Giá phòng: <span class="price">' . $gia_phong . '</span><br />';
				$html .= 'Diện tích: ' . $dien_tich . ' m<sup>2</sup><br />';
				$html .= '<div class="district">' . $quan_huyen->ten . '<br />';
				$html .= '<a class="send_email" title="Gửi email đến người đăng" href="#sendMailModal" data-toggle="modal" onclick="setVal(\'' . $action . '\', ' . $id_tin_rao . ', ' . $nguoi_dang->id . ', \'' . $email . '\')"><img src="' . $this->view->baseUrl(DEFAULT_PATH . '/img/icon_email.png') . '" alt="' . $email . '" /></a>';
				$html .= $htmlConPhong;
				$html .= '</div>';
				$html .= '</div>';
				$html .= '<div class="clear"></div></div>';
			}
			if ($checkVipEnd) {
				$html .= '</div>';
				$checkVipEnd = false;
			}
			
			// Đếm số items
			$totalItem = $model->countItem(null, $query);
			
			$html .= '<div class="pagenavi">';
			$html .= '<div class="pagination">';
			$panigator = $paginator->createPaginator($totalItem, $this->_paginator);
			$html .= $this->view->paginationControl($panigator, 'Sliding', 'pagination.phtml', array('ajax' => "'" . $id_tab . "', 'search/" . $record . "tt/" . $this->_arrParam['tt'] . "/qh/" . $this->_arrParam['qh'] . "/g/" . $this->_arrParam['g'] . "/dt/" . $this->_arrParam['dt'] . "/lp/" . $this->_arrParam['lp'] . "/sng/" . $this->_arrParam['sng'] . "', " . $loaitin . ", "));
			$html .= '</div>';
			$html .= '<br class="clear" /></div>';
			
			$data = array('content' => $html);
			echo json_encode($data);
		} else {
			echo "wrong check";
		}
	}
	
	public function sendforfriendAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$flag = false;
		
		if (isset($_POST)) {
			$transport = Zend_Registry::get('mailTransport');
			$m = new Zendhlt_Mail_Mail();
			$m	->setSubject("[Kênh nhà trọ] " . $this->_arrParam['tieu_de'] . " tại website Kênh nhà trọ")
				->addTo($this->_arrParam['email_to'])
				->setViewParam('hoten', $this->_arrParam['ten_nguoi_gui'])
				->setViewParam('emailfrom', $this->_arrParam['email_from'])
				->setViewParam('noidung', $this->_arrParam['thong_diep'])
				->setViewParam('link', $this->_arrParam['link_bai_viet'])
				->sendHtmlTemplate("forward.tpl", "UTF-8", $transport);
			$flag = true;
			$message = "Gửi thành công.";
		} else {
			$message = "wrong check";
		}
		$json = array('result' => $flag, 'message' => $message);
		echo json_encode($json);
	}
	
	public function sendmailAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$flag = false;
		
		if (isset($_POST)) {
			$formData = $this->getRequest()->getPost();
			$msg = new Default_Model_TableColumn_TinNhan();
			
			$nguoi_gui = 0;
			$auth = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('user'));
			if($auth->hasIdentity()){
				 $nguoi_gui = $auth->getIdentity()->id;
			}
			$msg->setNguoi_gui($nguoi_gui);
			$msg->setNguoi_nhan($formData['id_nguoi_dang']);
			$msg->setTieu_de($formData['subject']);
			$msg->setNoi_dung('<dt><a class="notDisplay" href="">...</a></dt><dd class="active"><p>' . $formData['message'] . '</p></dd>');
			$msg->setLoai(0);
			if ($formData['loai_bai_dang'] == 'ptrtinrao')
				$msg->setPtr_tin_rao($formData['id_bai_dang']);
			else
				$msg->setOgh_tin_rao($formData['id_bai_dang']);
			$msgMapper  = new Default_Model_TinNhanMapper();
			try {
				$transport = Zend_Registry::get('mailTransport');
				$m = new Zendhlt_Mail_Mail();
				$m	->setSubject("[Kênh nhà trọ] " . $formData['subject'] . " tại website Kênh nhà trọ")
					->addTo($formData['email_nguoi_dang'])
					->setViewParam('hoten', $formData['nguoi_gui'])
					->setViewParam('email', $formData['email_nguoi_gui'])
					->setViewParam('noidung', $formData['message'])
					->setViewParam('link', $formData['loai_bai_dang'] . '/view/id/' . $formData['id_bai_dang'])
					->sendHtmlTemplate("sendmail.tpl", "UTF-8", $transport);
				if ($msgMapper->save($msg)) {
					$flag = true;
					$message = "Gửi tin nhắn thành công";
				} else {
					$message = "Gửi tin nhắn thất bại! Vui lòng thử lại";
				}
			} catch (Exception $e) {
				$message = "Gửi tin nhắn thất bại! Vui lòng thử lại";
			}
		} else {
			$message = "wrong check";
		}
		$json = array('result' => $flag, 'message' => $message);
		echo json_encode($json);
	}
	
	public function replymailAction() {
		// Không hiển thị layout lên
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$flag = false;
		$auth = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('user'));
		if($auth->hasIdentity()) {
		
			if (isset($_POST)) {
				$nguoi_gui = $auth->getIdentity()->id;
				$formData = $this->getRequest()->getPost();
				$msgMapper = new Default_Model_TinNhanMapper();
				$loaitin = 'ptr_tin_rao';
				if ($formData['loai_bai_dang'] == 'oghtinrao')
					$loaitin = 'ogh_tin_rao';
				$item = $msgMapper->fetch($this->_arrParam, $loaitin . ' = ' . $formData['id_bai_dang'] . ' AND nguoi_nhan = ' . $formData['id_nguoi_dang'] . ' AND nguoi_gui = ' . $nguoi_gui, 0);
				
				if (!empty($item)) {
					$data = new Default_Model_TableColumn_TinNhan($item->toArray());
					$content = str_replace('notDisplay', 'tmsg', $item->noi_dung);
					$content = str_replace('...', '<img class="dot" src="/public/img/dot.gif" />', $content);
					$content = str_replace('active', 'old-msg', $content);
					$content .= '<dt><a class="notDisplay" href="">img</a></dt><dd class="active"><p>';
					$content .= $formData['message'];
					$content .= '</p></dd>';
					$data->setNoi_dung($content);
					$data->setDa_xem(0);
				} else {
					$data = new Default_Model_TableColumn_TinNhan();
					$data->setNguoi_gui($nguoi_gui);
					$data->setNguoi_nhan($formData['id_nguoi_dang']);
					$data->setTieu_de($formData['subject']);
					$data->setNoi_dung('<dt><a class="notDisplay" href="">...</a></dt><dd class="active"><p>' . $formData['message'] . '</p></dd>');
					$data->setLoai(0);
					if ($formData['loai_bai_dang'] == 'ptrtinrao')
						$data->setPtr_tin_rao($formData['id_bai_dang']);
					else
						$data->setOgh_tin_rao($formData['id_bai_dang']);
				}
				try {
					$transport = Zend_Registry::get('mailTransport');
					$m = new Zendhlt_Mail_Mail();
					$m	->setSubject("[Kênh nhà trọ] " . $formData['subject'] . " tại website Kênh nhà trọ")
						->addTo($formData['email_nguoi_dang'])
						->setViewParam('hoten', $formData['nguoi_gui'])
						->setViewParam('email', $formData['email_nguoi_gui'])
						->setViewParam('noidung', $formData['message'])
						->setViewParam('link', $formData['loai_bai_dang'] . '/view/id/' . $formData['id_bai_dang'])
						->sendHtmlTemplate("sendmail.tpl", "UTF-8", $transport);
					if ($msgMapper->save($data)) {
						$flag = true;
						$message = "Gửi tin nhắn thành công";
					} else {
						$message = "Gửi tin nhắn thất bại! Vui lòng thử lại";
					}
				} catch (Exception $e) {
					$message = "Gửi tin nhắn thất bại! Vui lòng thử lại";
				}
			} else {
				$message = "wrong check";
			}
		} else {
			$message = new Zend_Session_Namespace('message');
			$message->msg = '<ul class="messages"><li class="success">Hãy đăng nhập trước khi thực hiện thao tác này.</li></ul>';
			$message->redirect = '<script type="text/javascript">
							        var settimmer = 0;
							        $(function(){
							                window.setInterval(function() {
							                    var timeCounter = $("b[id=show-time]").html();
							                    var updateTime = eval(timeCounter)- eval(1);
							                    $("b[id=show-time]").html(updateTime);
							
							                    if(updateTime == 0){
							                        window.location = ("' . $this->view->baseUrl('') .'");
							                    }
							                }, 1000);
							
							        });
							    </script>';
			$message->redirect .= '<div class="redirect-msg"><div id="my-timer">Trang sẽ tự chuyển sau <b id="show-time">5</b> giây.</div><br />';
			$message->redirect .= '<p>Nếu không muốn đợt lâu vui lòng click vào <a href="' . $this->view->baseUrl('') .'">đây</a>.</p></div>';
			$this->_redirect('/message');
		}
		
		$json = array('result' => $flag, 'message' => $message);
		echo json_encode($json);
	}

	// Validate form forward
	public function validateforwardAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();

		$f = new Default_Form_SendForFriend();
		$f->isValid($this->_getAllParams());
		$json = $f->getMessages();
		header('Content-type: application/json');
		echo Zend_Json::encode($json);
	}

	// Validate form send mail
	public function validatesendmailAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();

		$f = new Default_Form_SendMail();
		$f->isValid($this->_getAllParams());
		$json = $f->getMessages();
		header('Content-type: application/json');
		echo Zend_Json::encode($json);
	}
	
	// Click quảng cáo
	public function clickadvAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$auth = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('user'));
		
		if($auth->hasIdentity()) {
			$num_point = $this->_getParam('user_point', 0);
			$id_user = $this->_getParam('user', 0);
			$id_page = $this->_getParam('page', 0);
			$id_position = $this->_getParam('position', 0);
			
			$model = new Default_Model_UsersMapper();
			$user = $model->fetchAll('id=' . $id_user, 'view');
			
			if ($user != null) {
				$point = array('diem' => $user->getDiem() + $num_point);
				$flag = $model->change($id_user, $point);
			} else {
				$flag = false;
			}
		} else {
			$flag = false;
		}
		
		$json = array('result' => $flag);
		echo json_encode($json);
	}
	
}